package com.ruoyi.socket.util.parse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.socket.util.MyDateUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/***
 *  @description 瑞博心电监护数据
 *  @author zhao
 *  @date: 2020/6/13
 *  @version: v1.0
 */
public class RBEcg {
    public static Map<String, Object> parseJSON2Map(String jsonString) {
        JSONObject jsonObject = JSON.parseObject(jsonString);
        JSONArray jsonArray = jsonObject.getJSONArray("Mindray");
        Map<String, Object> result = Maps.newHashMap();
        JSONObject deviceInfo = jsonArray.getJSONObject(0);
        // 设备
        String device = deviceInfo.getString("Device");
        // 设备ID
        String deviceID = deviceInfo.getString("DeviceID");
        result.put("deviceID", deviceID);
        result.put("device", device);
        String time_of_Message = deviceInfo.getString("Time_of_Message");
        String time = time_of_Message.substring(0, 14);
        LocalDateTime localDateTime = MyDateUtils.parseString(time);
        result.put("time_message", localDateTime);

        // 获取波形数据
        JSONObject dataJson = jsonArray.getJSONObject(4);
        String data_id = dataJson.getString("Data_ID");
        String wave_unit = dataJson.getString("Data_Units");
        String wave_data = dataJson.getString("Wave_data");
        // 心电监护12道数据
        List<String> waveDataType = Lists.newArrayList("MDC_ECG_ELEC_POTL_I", "MDC_ECG_ELEC_POTL_II", "MDC_ECG_ELEC_POTL_III", "MDC_ECG_ELEC_POTL_AVR", "MDC_ECG_ELEC_POTL_AVL", "MDC_ECG_ELEC_POTL_AVF", "MDC_ECG_ELEC_POTL_V1", "MDC_ECG_ELEC_POTL_V2", "MDC_ECG_ELEC_POTL_V3", "MDC_ECG_ELEC_POTL_V4", "MDC_ECG_ELEC_POTL_V5", "MDC_ECG_ELEC_POTL_V6");
        if (waveDataType.contains(data_id)) {
            result.put(data_id, wave_data);
            result.put("wave_unit", wave_unit);
        }
        // 其他数据

        // 心率
        if ("MDC_ECG_HEART_RATE".equals(data_id)) {
            result.put("ECG_Heart_Rate", wave_data);
        }
        // 呼吸频率
        if ("MDC_TTHOR_RESP_RATE".equals(data_id)) {
            result.put("Transthoracic_Respiration_Rate", wave_data);
        }

        // SpO2 1 Saturation 血氧饱和度
        if ("MDC_PULS_OXIM_SAT_O2".equals(data_id)) {
            result.put("SpO2_Saturation", wave_data);
        }
        // MDC_PULS_OXIM_PULS_RATE 脉率-PR
        if ("MDC_PULS_OXIM_PULS_RATE".equals(data_id)) {
            result.put("SpO2_Pulse_Rate", wave_data);
        }
        // MDC_BLD_PERF_INDEX 灌注指数-PI
        if ("MDC_BLD_PERF_INDEX".equals(data_id)) {
            result.put("SpO2_Perfusion_Index", wave_data);
        }

        // NIBP Systolic 无创血压收缩压
        if ("MDC_PRESS_CUFF_SYS".equals(data_id)) {
            result.put("NIBP_Systolic", wave_data);
        }

        // NIBP Diastolic舒张压
        if ("MDC_PRESS_CUFF_DIA".equals(data_id)) {
            result.put("NIBP_Diastolic", wave_data);
        }
        // NIBP Mean平均值
        if ("MDC_PRESS_CUFF_MEAN".equals(data_id)) {
            result.put("NIBP_Mean", wave_data);
        }
        return result;
    }

}
